import pymysql
import datetime,random

#连接数据库
db = pymysql.connect(
    host = '59.77.227.26',
    port = 3306,
    user = 'edptest',
    password = 'edptest',
    charset = 'utf8',
    database = 'fd_security'
)
#创建游标
cur = db.cursor()
#cur.execute(str("SELECT * FROM student WHERE id = '380424669821399040'"))


def randomtimes(start, end, n, frmt="%Y-%m-%d %H:%M:%S"):
    stime = datetime.datetime.strptime(start, frmt)
    etime = datetime.datetime.strptime(end, frmt)
    time_datetime=[random.random() * (etime - stime) + stime for _ in range(n)]
    time_str=[t.strftime(frmt) for t in time_datetime]
    return time_str
suiji = randomtimes('2021-11-01 01:00:00','2021-11-30 23:00:00',10)
#print(suiji)


first=1
second=1
while first < 10:
    #插入数据
    cur.execute(
        "insert into threat_warning (warning_date) values ('randomtimes('2021-11-01 01:00:00','2021-11-30 23:00:00',10)')");
    first+=1
    second+=2
    #提交数据
    db.commit()
    print('ok')
db.close()
cur.close()
